const Router = require('koa-router');
const router = new Router();
const tool = require('../../tools/tool')
const {db, QueryTypes} = require("../../models/db");

// 政策兑付
router.post('/pay/zcdflist', async ctx => {
    let user = ctx.user
    let body = ctx.request.body
    let formData = body.formData
    let tenantId = user.tenantId;
    let beginDate = formData.beginDate ? formData.beginDate: '';
    let endDate = formData.endDate?formData.endDate:'';
    let buId = formData.buId?formData.buId:0;
    let brandId = formData.brandId?formData.brandId:0;
    let invId = formData.invId?formData.invId:0;
    let billNo = formData.billNo?formData.billNo:'';
    let pageSize = body.pageSize ?body.pageSize:200;
    let currentPage = body.currentPage ?body.currentPage:1;
    let skip = (currentPage - 1) *pageSize;
    let sort = body.sort ? body.sort : []
    let excel = body.excel
    let resql = `
           select a.id, a.tenantId, a.billDate, a.beginDate, a.endDate, buId,b.name buName, a.brandId,c.brandName, invId,c.name invName, dfType, accId,
                  va.name accName, a.amount, billState,a.cby, a.createdAt
                  from send_df a inner join contact b on a.tenantId=b.tenantId and a.buId=b.id 
                  left join v_goods c on a.tenantId=c.tenantId and a.invId=c.id  
                  left join v_account va on a.tenantId=va.tenantId and a.accId=va.id
                    where a.tenantId=${tenantId} and billState='accounted'
        `
    if(beginDate) {
        resql += ` and a.billDate>='${beginDate}' `
    }
    if(endDate) {
        resql += ` and a.billDate<='${endDate}' `
    }
    if(buId) {
        resql += ` and a.buId=${buId} `
    }
    if(brandId) {
        resql += ` and a.brandId=${brandId} `
    }
    if(invId) {
        resql += ` and a.invId=${invId} `
    }

    resql += ` order by billDate desc,buName `
    let res = await db.query(resql, {type: QueryTypes.SELECT})
    return ctx.suc('', {
        list: res
    })

})



module.exports = router;
